<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script>
    const imgs = document.getElementsByTagName('img')  
    // 获取可视区域的高度
    const viewHeight = window.innerHeight || document.documentElement.clientHeight
    // num统计加载到哪张图片
    let num = 0
    function lazyload(){
      for(let i = num, len=imgs.length; i<len; i++){
        // 可视区域高度减去图片元素距离可视区域顶部的高度
        let distance = viewHeight - imgs[i].getBoundingClientRect().top
        // 如果可视区域高度大于元素顶部距离，说明图片元素已经露出
        if(distance >= 0){
          imgs[i].src = imgs[i].getAttribute('data-src')
          num = i + 1
        }
      }
    }
    window.addEventListener('scroll', lazyload, false)
  </script>
</body>
</html>